package com.ctos.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctos.common.Result;
import com.ctos.entity.AddressBook;
import com.ctos.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 新增收货地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result<String> saveAddress(@RequestBody AddressBook addressBook, HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        addressBook.setUserId(userId);
        addressBookService.save(addressBook);

        return Result.success("success");
    }

    /**
     * 查询当前用户的所有地址
     * @param session
     * @return
     */
    @GetMapping("/list")
    public Result<List> selectList(HttpSession session){
        LambdaQueryWrapper<AddressBook> qw =new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, session.getAttribute("user"));
        return Result.success(addressBookService.list(qw));

    }

    /**
     * 设置默认收货地址
     * @param addressBook
     * @param session
     * @return
     */
    @PutMapping("/default")
    public Result<String> setDefault(@RequestBody AddressBook addressBook,HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        addressBookService.setDefault(addressBook.getId(), userId);

        return Result.success("success");
    }

    /**
     * 获取默认收货地址
     * @param session
     * @return
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefault(HttpSession session){
        Long userId = (Long) session.getAttribute("user");
        LambdaQueryWrapper<AddressBook> qw = new LambdaQueryWrapper<>();
        qw.eq(AddressBook::getUserId, userId).eq(AddressBook::getIsDefault, 1);
        AddressBook defaultAddress = addressBookService.getOne(qw);
        if (defaultAddress==null){
            return Result.error("暂无默认信息");
        }
        return Result.success(defaultAddress);
    }

    /**
     * 回显地址数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<AddressBook> echoData(@PathVariable Long id){
        AddressBook user = addressBookService.getById(id);
        if (user!=null){
            return Result.success(user);
        }
        return Result.error("未知错误");
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping()
    public Result<String> modifyAddress(@RequestBody AddressBook addressBook){

        addressBookService.updateById(addressBook);
        return Result.success("success");
    }

    /**
     * 删除地址
     * @param id
     * @return
     */
    @DeleteMapping()
    public Result<String> delete(@RequestParam("ids") Long id){
        addressBookService.removeById(id);
        return Result.success("success");
    }

}
